<?php

class GL_Upload extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->layout->setLayout("home/layout");
        if ($this->permission() == 3 || $this->permission() == 1) redirect('../');
    }

    function index(){
        $data['cats'] = $this->category->get_all();
        $this->layout->view('/home/upload',$data);
    }
    function edit($id){
        $data['cats'] = $this->category->get_all();
        $data['product'] = $this->product->get($id)[0];
        $data['tags'] = $this->tag->get_tags($id);
        $data['images']=$this->product->get_images($id);

        $this->layout->view('/home/edit',$data);
    }
    function new_item(){
        echo $this->product->save();
    }

    function save_item(){
        echo $this->product->update();
    }

    function save_image(){

        $id = $this->image->save();

        $allowedExts = array("gif", "jpeg", "jpg", "png");
        $temp = explode(".", $_FILES["file"]["name"]);
        $extension = end($temp);
        if ((($_FILES["file"]["type"] == "image/gif")
                || ($_FILES["file"]["type"] == "image/jpeg")
                || ($_FILES["file"]["type"] == "image/jpg")
                || ($_FILES["file"]["type"] == "image/pjpeg")
                || ($_FILES["file"]["type"] == "image/x-png")
                || ($_FILES["file"]["type"] == "image/png"))
            && in_array($extension, $allowedExts)) {
            $file_name = $_SERVER["DOCUMENT_ROOT"]."/GraphicLand/public/home/img/product/" . $id.'.jpg';
            if(file_exists($file_name)) {
                chmod($file_name,0755); //Change the file permissions if allowed
                unlink($file_name); //remove the file
            }
            if ($_FILES["file"]["error"] > 0) {
                echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
            } else {
                move_uploaded_file($_FILES["file"]["tmp_name"], $file_name);
                echo 1;
            }
        } else {
            $this->image->remove($id);
            echo 0;
        }
    }

    function remove_image(){
        $id = $this->input->post('id');
        $this->image->remove($id);
    }

    function save_product_file(){

        $id = $this->input->post('product_id');

        $allowedExts = array("zip", "ZIP");
        $temp = explode(".", $_FILES["file"]["name"]);
        $extension = end($temp);
        if ((($_FILES["file"]["type"] == "application/zip"))
            && in_array($extension, $allowedExts)) {
            $file_name = $_SERVER["DOCUMENT_ROOT"]."/GraphicLand/ProductFile/" . $id.'.zip';
            if(file_exists($file_name)) {
                chmod($file_name,0755); //Change the file permissions if allowed
                unlink($file_name); //remove the file
            }
            if ($_FILES["file"]["error"] > 0) {
                echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
            } else {
                move_uploaded_file($_FILES["file"]["tmp_name"], $file_name);
                echo 1;
            }
        } else {
            echo 0;
        }
    }
}